# -*- encoding: utf-8 -*-
"""
@File    : User_channel_pie.py
@Time    : 2020/3/25 6:44 下午
@Author  : david wang
@Email   : 3063737@qq.com
@Software: PyCharm
"""
import pandas as pd
import numpy as np
from pandas import DataFrame
import pyecharts.options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType

import setting

# 定义文件路径
filename = setting.export_file+'User_channel_pie.html'
iopath = setting.resource_file+'User_loginout_his.xlsx'

# 读取xls文件
df_brower = pd.DataFrame(pd.read_excel(iopath, sheet_name='browser'))
df_x = df_brower['浏览器'].to_list()
df_y = df_brower['合计'].to_list()
da0_brower = [list(i) for i  in zip(df_x,df_y)]    ## 用zip将对象迭代组合成元组，并用list罗列出来

(
    Pie(init_opts=opts.InitOpts(theme=ThemeType.WHITE, height="800px", width="1500px"))
    .add(
        series_name="浏览器分布",
        # radius：扇区圆心角展现数据的百分比，半径展现数据的大小
        # area：所有扇区圆心角相同，仅通过半径展现数据大小
        # radius=["50%", "50%"],   ##圆环粗细和大小
        radius="40%",              ## 设置饼图的大小
        # rosetype="radius",       ## 设置rosetype，则绘制玫瑰图（南丁格尔图），否则为普通饼图
        center=["50%", "50%"],     ## 设置饼图中心的位置
        data_pair=da0_brower,
        label_opts=opts.LabelOpts(
            position="outside",
            formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c}  {per|{d}%}  ",
            background_color="#eee",
            border_color="#aaa",
            border_width=1,
            border_radius=4,
            rich={
                "a": {"color": "#999", "lineHeight": 22, "align": "center"},
                "abg": {
                    "backgroundColor": "#e3e3e3",
                    "width": "100%",
                    "align": "right",
                    "height": 22,
                    "borderRadius": [4, 4, 0, 0],
                },
                "hr": {
                    "borderColor": "#aaa",
                    "width": "100%",
                    "borderWidth": 0.5,
                    "height": 0,
                },
                "b": {"fontSize": 16, "lineHeight": 33},
                "per": {
                    "color": "#eee",
                    "backgroundColor": "#334455",
                    "padding": [2, 4],
                    "borderRadius": 2,
                },
            },
        ),
    )
        .set_global_opts(title_opts=opts.TitleOpts(title="浏览器占比", subtitle="M站"),
                         legend_opts=opts.LegendOpts(pos_left="right", orient="vertical"))
    .set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
        )
    )
    .render(filename)
)
import webbrowser
webbrowser.open_new_tab( 'file://'+filename)

